package com.chen.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.reggie.common.R;
import com.chen.reggie.dto.OrderDto;
import com.chen.reggie.pojo.OrderDetail;
import com.chen.reggie.pojo.Orders;
import com.chen.reggie.service.OrderDetailService;
import com.chen.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 用户下单： 前端只传了{remark: "", payMethod: 1, addressBookId: "1526445853153288193"}
     * 原因：因为用户是登录的，所以可以查到用户的id（BaseContext，service层处理），然后再根据用户的id去查对应的
     *      购物车数据，进而生成订单数据
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders ){

        log.info("订单数据：{}" , orders );

        orderService.submit( orders );

        return R.success("下单成功！");

    }

    //用户查看全部订单
    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        //orderDto分页对象
        Page<OrderDto> orderDtoPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        Page<Orders> ordersPage = orderService.page(pageInfo, ordersLambdaQueryWrapper);


        List<Orders> records = ordersPage.getRecords();
        List<OrderDto> collect = records.stream().map((item) -> {
            //包含订单和详细订单集合
            OrderDto orderDto = new OrderDto();
            Long orderId = item.getId();
            List<OrderDetail> ordersDetail = orderDetailService.getOrdersDetail(orderId);
            BeanUtils.copyProperties(item, orderDto);
            orderDto.setOrderDetails(ordersDetail);
            return orderDto;
        }).collect(Collectors.toList());
        orderDtoPage.setRecords(collect);

        return R.success(orderDtoPage);
    }
    @PutMapping
    public R<String> updateState(@RequestBody Map<String,String> map){
        LambdaUpdateWrapper<Orders> ordersLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        ordersLambdaUpdateWrapper.eq(Orders::getId,map.get("id")).set(Orders::getStatus,map.get("status"));
        orderService.update(ordersLambdaUpdateWrapper);
        return R.success("订单状态修改成功");
    }
    @GetMapping("/page")
    public R<Page> orderPage(int page, int pageSize, Long number, String beginTime,String endTime){
        //条件构造器
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.eq(number!=null,Orders::getNumber,number);
        ordersLambdaQueryWrapper.between(beginTime!=null&&endTime!=null,Orders::getOrderTime,beginTime,endTime);
        ordersLambdaQueryWrapper.orderByDesc(Orders::getOrderTime);
        Page<Orders> OrdersPage = orderService.page(pageInfo, ordersLambdaQueryWrapper);

        return R.success(OrdersPage);

    }
}
